/**   
 * Copyright © 2019 eSunny Info. Tech Ltd. All rights reserved.
 * 
 * @Description:TODO(功能)
 * @className FileServiceImpl.java
 * @Package: com.erp.service 
 * @author: yj   
 * @date: 2019年3月2日 下午2:29:24 
 */
package com.erp.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.erp.dao.FileDaoImpl;
import com.erp.po.Files;
import com.erp.util.DBHelper;


public class FileServiceImpl implements FileService {

	
	private FileDaoImpl fdi;
	
	public FileServiceImpl() {
		fdi = new FileDaoImpl();
	}
	
	/* 
	 * 添加文件数据
	 */
	@Override
	public int addFile(Files file) {
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			
			// 开启事务处理
			conn.setAutoCommit(false);
			int i = fdi.insertFile(conn, file);
			conn.commit();
			conn.setAutoCommit(true);
			
			return i;
		} catch (SQLException e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			fdi.close();
		}
		return 0;
	}

	/* 
	 * 根据 userName 查询文件
	 */
	@Override
	public List<Files> findFileByUserName(String userName) {
		
		Connection conn = null;
		ArrayList<Files> list = new ArrayList<Files>();
		
		try {
			conn = DBHelper.getConnection();
			
			ResultSet set = fdi.selectFileByUserName(conn, userName);
			while (set.next()) {
				int id = set.getInt("id");
				String fileName = set.getString("file_name");
				String fileDesc = set.getString("file_desc");
				String fileAutoName = set.getString("file_auto_name");
				String userNames = set.getString("user_name");
				
				list.add(new Files(id,fileName, fileDesc, fileAutoName, userNames));
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			fdi.close();
		}
		
		return null;
	}

	/* 
	 * 根据 id 查询文件
	 */
	@Override
	public Files findFileById(int id) {
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			
			ResultSet set = fdi.selectFileById(conn, id);
			while (set.next()) {
				int Id = set.getInt("id");
				String fileName = set.getString("file_name");
				String fileDesc = set.getString("file_desc");
				String fileAutoName = set.getString("file_auto_name");
				String userName = set.getString("user_name");
				
				return new Files(Id, fileName, fileDesc, fileAutoName, userName);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			fdi.close();
		}
		
		return null;
	}

	/* 
	 * 根据 id 删除文件数据
	 */
	@Override
	public boolean removeFileById(int id) {
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			
			//开启事务
			conn.setAutoCommit(false);
			fdi.deleteFileById(conn, id);
			conn.commit();
			conn.setAutoCommit(true);
			
			return true;
		} catch (SQLException e) {
			try {
				conn.rollback();
				conn.setAutoCommit(true);
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			e.printStackTrace();
		} finally {
			fdi.close();
		}
		return false;
	}

}
